package com.jingbaoduo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jingbaoduo.common.R;
import com.jingbaoduo.domain.User;
import com.jingbaoduo.service.UserService;
import com.jingbaoduo.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user, HttpSession session){
        //获取手机号
        String phone = user.getPhone();
        log.info("手机号:{}",phone);
        //随机四位验证码
        if (StringUtils.isNotEmpty(phone)){
            String code = ValidateCodeUtils.generateValidateCode(4).toString();
            log.info("验证码:{}",code);

            //将验证码存储到session中
            session.setAttribute(phone,code);

            //将生成的验证码缓存到redis中,并且设置有效期为5分钟
            redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);

            return R.success("手机验证码发送成功");
        }
    return R.error("短信发送失败");

    }

    @PostMapping("/login")
    public R<User> login(@RequestBody Map map,HttpSession session){
        log.info("验证登录");
        //获取手机号
        String phone =(String) map.get("phone");
        //获取验证码
        String code = (String) map.get("code");
        log.info("验证码:{}",code);

        //从session中获取验证码

//        String codeInSession = (String) session.getAttribute(phone);

        //从Redis中获取缓存的验证码
        String codeInSession = (String)redisTemplate.opsForValue().get(phone);

        //进行验证码验证
        if (codeInSession != null && codeInSession.equals(code)){
            //对比成功则登录成功
            LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(User::getPhone,phone);

            User user = userService.getOne(queryWrapper);

            if (user == null){
                //判断当前用户是否为新用户,如果为新用户则自动完成注册
                User user1 = new User();
                user1.setPhone(phone);
                userService.save(user1);
                session.setAttribute("user",user1.getUserId());
            }else {
                session.setAttribute("user",user.getUserId());
            }

            //如果用户登录成功,删除redis中缓存的验证码
            redisTemplate.delete(phone);

            return R.success(user);
        }
        return R.error("登录失败");

    }

    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        log.info("退出登录");
        request.getSession().removeAttribute("user");
        return R.success("退出成功");
    }

}
